var softVersion = "MS4wLjU=";
var softVersionId = "MjAxODA0MDk=";

var token = $.cookie("token");
var userId = $.cookie("userId");

var currentHostUrl =  window.location.protocol + "//" + window.location.host;
var apiHostUrl = "http://127.0.0.1:8020"
// var apiHostUrl = "https://api.junxtec.com"

function initLoadingAnim() {
    // 插入loading动画
    var html = "";
    html += '<div class="js_loading">';
    html += '<div class="mask"></div>';
    html += '<div class="loading">';
    html += '<span><div class="loading-title">数据加载中..&nbsp;</div>';
    html += '<i class="fa fa-spinner fa-spin fa-2x"></i></span>';
    html += '</div>';
    html += '</div>';
    $(".layui-layout-body").append(html);
}

/**
 * 封装Ajax请求
 * @param requestUrl 请求地址
 * @param requestData 请求参数
 * @param requestMethod 请求类型
 * @param isLoadingAnim 是否显示加载动画
 */
function ajax(requestUrl, requestMethod, requestData, isLoadingAnim, isAsync) {

    if (isLoadingAnim) {
        $(".js_loading").show();
    }

    if (token === "undefined") {
        token = "";
    }

    return $.ajax({
        headers: {'Authorization': token},
        url: apiHostUrl + requestUrl,
        type: requestMethod,
        data: requestData == null ? null : JSON.stringify(requestData),
        async : isAsync,
        dataType: "json",
        contentType: "application/json"
    }).then(function (result) {
            $(".js_loading").hide();
            // 成功回调
            if (result.errorCode.toString() === "0") {
                return result.returnData;
            }
            // 未登录
            else if (result.errorCode.toString() === "401") {
                // layer.alert(result.errorCode + ":" + result.errorMsg,
                //     {
                //         id: 'layui-type-error',
                //         title: "提示",
                //         btn: ['确定']
                //     }, function () {
                //         window.location = currentHostUrl;
                //     });
                window.location = currentHostUrl;
            }
            // 失败回调
            else {
                layer.msg(result.errorCode + ":" + result.errorMsg);
            }
        },
        function (result) {
            $(".js_loading").hide();
            if (result.errorCode == null || result.errorMsg == null) {
                layer.msg("网络连接失败");
            }
            else {
                layer.msg(result.errorCode + ":" + result.errorMsg);
            }
        });

}